package test.org.offsync.util

import org.junit.runner.RunWith
import org.offsync.util.Window
import org.specs._
import org.specs.runner._

@RunWith(classOf[JUnitSuiteRunner])
class SpecWindow extends SpecificationWithJUnit("test.org.offsync.util.SpecWindow") {
  "Array(0, 1, 2, 3)" should {
    val array = Array(0, 1, 2, 3)
    "successfully construct Window" in {
      val window = Window to array
      "which cannot create subwindow from index -1" in {
        window.from(-1) must throwA[AssertionError]
      }
      "which can create subwindow from index 0" in {
        val window0 = window from 0
        "which cannot create subwindow until index -1" in {
          window0.until(-1) must throwA[AssertionError]
        }
        "which can create subwindow until index 0" in {
          val window00 = window0 until 0
          window00.length must_== 0
          window00(-1) must throwA[AssertionError]
          window00(0) must throwA[AssertionError]
          window00(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 1" in {
          val window01 = window0 until 1
          window01.length must_== 1
          window01(-1) must throwA[AssertionError]
          window01(0) must_== 0
          window01(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 2" in {
          val window02 = window0 until 2
          window02.length must_== 2
          window02(-1) must throwA[AssertionError]
          window02(0) must_== 0
          window02(1) must_== 1
          window02(2) must throwA[AssertionError]
        }
        "which can create subwindow until index 3" in {
          val window03 = window0 until 3
          window03.length must_== 3
          window03(-1) must throwA[AssertionError]
          window03(0) must_== 0
          window03(1) must_== 1
          window03(2) must_== 2
          window03(3) must throwA[AssertionError]
        }
        "which can create subwindow until index 4" in {
          val window04 = window0 until 4
          window04.length must_== 4
          window04(-1) must throwA[AssertionError]
          window04(0) must_== 0
          window04(1) must_== 1
          window04(2) must_== 2
          window04(3) must_== 3
          window04(4) must throwA[AssertionError]
        }
        "which cannot create subwindow until index 5" in {
          window0.until(5) must throwA[AssertionError]
        }
      }
      "which can create subwindow from index 1" in {
        val window1 = window from 1
        "which cannot create subwindow until index -1" in {
          window1.until(-1) must throwA[AssertionError]
        }
        "which can create subwindow until index 0" in {
          val window10 = window1 until 0
        }
        "which can create subwindow until index 1" in {
          val window11 = window1 until 1
        }
        "which can create subwindow until index 2" in {
          val window12 = window1 until 2
        }
        "which can create subwindow until index 3" in {
          val window13 = window1 until 3
        }
        "which cannot create subwindow until index 4" in {
          window1.until(4) must throwA[AssertionError]
        }
      }
      "which can create subwindow from index 2" in {
        window.from(2)
      }
      "which can create subwindow from index 3" in {
        window.from(3)
      }
      "which can create subwindow from index 4" in {
        window.from(4)
      }
      "which cannot create subwindow from index 5" in {
        window.from(5) must throwA[AssertionError]
      }
    }
  }
}
